package org.csu.mypetstore.service;


import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.LineItem;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.domain.Sequence;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.LineItemMapper;
import org.csu.mypetstore.persistence.OrderMapper;
import org.csu.mypetstore.persistence.SequenceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderService {

      @Autowired
      private ItemMapper itemMapper;
      @Autowired
      private OrderMapper orderMapper;
      @Autowired
      private SequenceMapper sequenceMapper;
      @Autowired
      private LineItemMapper lineItemMapper;

      @Transactional
      public void insertOrder(Order order) {
            order.setOrderId(getNextId("ordernum"));
            for (int i = 0; i < order.getLineItems().size(); i++) {
                  LineItem lineItem = (LineItem) order.getLineItems().get(i);
                  String itemId = lineItem.getItemId();
                  Integer increment = new Integer(lineItem.getQuantity());
                  Map<String, Object> param = new HashMap<String, Object>(2);
                  param.put("itemId", itemId);
                  param.put("increment", increment);
                  itemMapper.updateInventoryQuantity(param);
            }

            orderMapper.insertOrder(order);
            orderMapper.insertOrderStatus(order);
            for (int i = 0; i < order.getLineItems().size(); i++) {
                  LineItem lineItem = (LineItem) order.getLineItems().get(i);
                  lineItem.setOrderId(order.getOrderId());
                  lineItemMapper.insertLineItem(lineItem);
            }
      }

      @Transactional
      public Order getOrder(int orderId) {
            Order order = orderMapper.getOrder(orderId);
            order.setLineItems(lineItemMapper.getLineItemsByOrderId(orderId));

            for (int i = 0; i < order.getLineItems().size(); i++) {
                  LineItem lineItem = (LineItem) order.getLineItems().get(i);
                  Item item = itemMapper.getItem(lineItem.getItemId());
                  item.setQuantity(itemMapper.getInventoryQuantity(lineItem.getItemId()));
                  lineItem.setItem(item);
            }

            return order;
      }

      public List<Order> getOrdersByUsername(String username) {
            return orderMapper.getOrdersByUsername(username);
      }

      public int getNextId(String name) {
            Sequence sequence = new Sequence(name, -1);
            sequence = (Sequence) sequenceMapper.getSequence(sequence);
            if (sequence == null) {
                  throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
                          + " sequence).");
            }
            Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
            sequenceMapper.updateSequence(parameterObject);
            return sequence.getNextId();
      }


      //查询用户的订单
      public List<Order> getAllOrders(String userId) {
            return orderMapper.selectOrders("%"+userId+"%");
      }

      //修改订单
      public void update(Order order) {
            orderMapper.updateOrder(order);
      }

      //删除订单
      public void deleteOrder(int orderId) {
            orderMapper.deleteOrder(orderId);
      }

      //修改发货状态
      public void updateStatus(int orderId,boolean isShipped) {
            System.out.println("小鲫瓜子" + isShipped);
            if(isShipped == true)
                  orderMapper.updateCancelStatus(orderId);
            if(isShipped == false)
                  orderMapper.updateConfirmStatus(orderId);
      }

      public Order selectOrderById(int orderId){
            return orderMapper.selectOrderById(orderId);
      }



}
